---
title: "Coronavirus Immigrant Foodworkers Crosstalk"
author: "Joe Yerardi"
date: "6/19/2020"
output:
  flexdashboard::flex_dashboard:
    theme: paper
    source_code: embed
---

```{r setup, include=FALSE}
# setting up R Markdown options

# We want to hide the code and only see the results
knitr::opts_chunk$set(echo = F)

# We don't want to see any warnings from our code
knitr::opts_chunk$set(warning = F)

# We don't want to see any messages
knitr::opts_chunk$set(message = F)
```

```{r install_packages}
# install.packages("flexdashboard")
# This function checks if you don't have the correct packages installed yet
# If not, it will install it for you
packages <- c("crosstalk", "DT", "flexdashboard", "rgdal", "geojsonsf", "leaflet", "sf", "tidyverse")

if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
  install.packages(setdiff(packages, rownames(installed.packages())), repos = "http://cran.us.r-project.org")
}

library("crosstalk")
library("DT")
library("flexdashboard")
library("geojsonsf")
library("leaflet")
library("rgdal")
library("sf")
library("tidyverse")
```

```{r set global variables}
# Set working directory
setwd("C:/Users/joeye/Desktop/covid-immigrant-foodworkers/")
```

```{r load_and_clean_data}
# Import the sf data
citizenship_ethnicity_by_puma <- read_sf("data/exported/citizenship_ethnicity_by_puma.json") %>% 
  as("Spatial") # Convert sf object to sp object to allow map filtering

# 
sd_map <- SharedData$new(citizenship_ethnicity_by_puma)
sd_df <- SharedData$new(as.data.frame(citizenship_ethnicity_by_puma@data), group = sd_map $groupName())

#shared_data <- SharedData$new(citizenship_ethnicity_by_puma)
```

Distribution of foreign-born workers in food-related industries {data-icon="ion-stats-bars"}
=====================================  

Column {data-width=200}
-------------------------------------

### Filters

```{r filter_section}
filter_select(
  id = "state",
  label = "State",
  sharedData = sd_df,
  group = ~state
  )
bscols(
  filter_slider(
    id = "pct_total_foreign",
    label = "Percent Foreign",
    sharedData = sd_df,
    column = ~pct_total_foreign,
    step = 5,
    round = TRUE,
    sep = "",
    ticks = FALSE
  )
)
bscols(
  filter_slider(
    id = "pct_total_foreign_hispanic",
    label = "Percent Foreign Hispanic",
    sharedData = sd_df,
    column = ~pct_total_foreign_hispanic,
    step = 5,
    round = TRUE,
    sep = "",
    ticks = FALSE
  )
)
```

Column {data-width=800}
-------------------------------------

### Datatable

```{r filterable_table}
sd_df %>% 
  DT::datatable(
    class = "compact")
```

### Interactive map

```{r interactive_map}
pal <- colorNumeric("YlOrRd",
                    domain = citizenship_ethnicity_by_puma$pct_total_foreign)

sd_map %>% 
  leaflet() %>% 
  setView(-98.5795, 39.8283, zoom = 3) %>% 
  addTiles() %>% 
  addPolygons(fillColor = ~pal(citizenship_ethnicity_by_puma$pct_total_foreign),
              fillOpacity = 1, 
              weight = 0.9, 
              smoothFactor = 0.2, 
              stroke=TRUE,
              color="white",
              popup = ~ puma,
              label = ~ puma) %>% 
  addLegend(pal = pal,
            values = citizenship_ethnicity_by_puma$pct_total_foreign,
            position = "bottomright",
            title = "Percent Total Foreign")
```